package com.wzg.appframe.view;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.BitmapShader;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.Shader;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.Spinner;

import com.wzg.appframe.R;

import butterknife.BindView;
import butterknife.ButterKnife;

public class LoginActivity extends BaseActivity {

    @BindView(R.id.spinner)
    Spinner ztSP;

    @BindView(R.id.avatar)
    ImageView avatarView;

    @Override
    public void initViews() {
        setContentView(R.layout.activity_login);
        ButterKnife.bind(this);

        Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.head_icon);
        Bitmap roundedBitmap = getRoundedBitmap(bitmap, bitmap.getWidth()/2);  // 圆角的半径
        avatarView.setImageBitmap(roundedBitmap);
    }

    @Override
    public void initData() {
// 设置数据源
        String[] items = {"Option 1", "Option 2", "Option 3"};
        ArrayAdapter<String> adapter = new ArrayAdapter<>(this, android.R.layout.simple_spinner_item, items);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        ztSP.setAdapter(adapter);
    }


    /**
     * 获取带圆角的Bitmap
     */
    public Bitmap getRoundedBitmap(Bitmap bitmap, float radius) {
        // 创建一个空白Bitmap
        Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Bitmap.Config.ARGB_8888);

        // 创建一个Canvas来绘制
        Canvas canvas = new Canvas(output);

        // 创建画笔
        Paint paint = new Paint();
        paint.setAntiAlias(true);  // 启用抗锯齿
        paint.setShader(new BitmapShader(bitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP));  // 使用BitmapShader来渲染图片

        // 设置圆角矩形的RectF
        Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
        RectF rectF = new RectF(rect);

        // 绘制圆角矩形
        canvas.drawRoundRect(rectF, radius, radius, paint);

        // 返回带圆角的Bitmap
        return output;
    }
}
